import java.util.*;

public class BinaryTree {
    static class TreeNode {
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        return A;
    }

    //前序遍历
    public void perOrder(TreeNode root) {
        if(root == null) {
            return ;
        }

        System.out.print(root.val + " ");
        perOrder(root.left);
        perOrder(root.right);
    }

    //前序遍历(非递归)
    public void perOrderNor(TreeNode root) {
        if(root == null) {
            return ;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;

        while(cur != null || !stack.empty()) {
            while(cur != null) {
                stack.push(cur);
                System.out.print(cur.val + " ");
                cur = cur.left;
            }
            //cur==null的时候
            TreeNode top = stack.pop();
            cur = top.right;
        }
    }

    //中序遍历（非递归）
    public void inOrderNor(TreeNode root) {
        if(root == null) {
            return ;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;

        while(cur != null || !stack.empty()) {
            while(cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            //cur==null的时候
            TreeNode top = stack.pop();
            System.out.print(top.val + " ");
            cur = top.right;
        }
    }

    //中序遍历
    public void inOrder(TreeNode root) {
        if(root == null) {
            return ;
        }

        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }


    //后序遍历（非递归）
    public void postOrderNor(TreeNode root) {
        if(root == null) {
            return ;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        TreeNode prev = null;

        while(cur != null || !stack.empty()) {
            while(cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            //cur==null的时候
            TreeNode top = stack.peek();
            if(top.right == null || top.right == prev) {
                System.out.print(top.val + " ");
                prev = top;
                stack.pop();
            }
            else {
                cur = top.right;
            }
        }
    }


    //后序遍历
    public void postOrder(TreeNode root) {
        if(root == null) {
            return ;
        }

        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val + " ");
    }


    //二叉树的结点个数(递归)
    public int size1(TreeNode root) {
        int ret =0 ;
        if(root == null) return ret;

        return size(root.left) + size(root.right) + 1;
    }



    //二叉树的结点个数(递归)
    static int usedSide = 0 ;
    public int size(TreeNode root) {

        if(root == null) return usedSide;

        usedSide++;
        size(root.left);
        size(root.right);
        return usedSide;
    }


    //二叉树的叶子结点的个数（非递归）
    public static int leafSize = 0;
    public int getTreeNodeCount(TreeNode root) {
        if(root == null) return 0;

        if(root.left == null && root.right == null)
        return leafSize++;

        getTreeNodeCount(root.left);
        getTreeNodeCount(root.right);
        return leafSize;
    }


    //二叉树的叶子结点的个数（递归）
    public int getTreeNodeCount1(TreeNode root) {
        if(root == null) return 0;

        if(root.left == null && root.right == null)
            return 1;

        return getTreeNodeCount1(root.left) + getTreeNodeCount1(root.right);
    }


    //求第k层结点的个数
    public int getKLevelNodeCount(TreeNode root, int k) {
        if(root == null) {
            return 0;
        }

        if(k == 1)
        {
            return 1;
        }

        return getKLevelNodeCount(root.left,k-1) + getKLevelNodeCount(root.right,k-1);
    }


    //求整棵树的深度
    public int maxDepth(TreeNode root) {
        if(root == null) {
            return 0;
        }

        int leftH = maxDepth(root.left);
        int rightH = maxDepth(root.right);
        return (leftH > rightH ? leftH : rightH) + 1;
    }


    //找指定元素结点
    public TreeNode find(TreeNode root, int val) {
        if(root == null) {
            return null;
        }
        if(root.val == val) {
            return root;
        }

        TreeNode leftL = find(root.left,val);
        if(leftL != null)
        {
            return leftL;
        }

        TreeNode rightR = find(root.right,val);
        if(rightR != null)
        {
            return rightR;
        }
        return null;
    }

    //层序遍历（非递归）
    public void levelOrder(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        if(root != null) {
            queue.offer(root);
        }

        while(!queue.isEmpty()) {
            TreeNode top = queue.poll();
            System.out.print(top.val + " ");
            if(top.left != null) {
                queue.offer(top.left);
            }
            if(top.right != null) {
                queue.offer(top.right);
            }
        }
    }

    //层序遍历(分层，类似于二维数组)
//    public List<List<Integer>> levelOrder2(TreeNode root) {
//        List<List<Integer>> ret = new ArrayList<>();
//        if(root == null)
//            return ret;
//        Queue<TreeNode> queue = new LinkedList<>();
//        queue.offer(root);
//
//        while(!queue.isEmpty()) {
//            int size = queue.size(); //这一层结点的个数
//            List<Integer> list = new ArrayList<>();
//            while(size != 0) {
//                TreeNode top = queue.poll();
//                //System.out.print(top.val + " ");
//                list.add(top.val);
//                if(top.left != null) {
//                    queue.offer(top.left);
//                }
//                if(top.right != null) {
//                    queue.offer(top.right);
//                }
//                size--;
//            }
//            ret.add(list);
//        }
//        return ret;
//    }


    //判断是否为完全二叉树
    public boolean isCompleteTree(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        if(root != null) {
            queue.offer(root);
        }

        while(!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            if(cur != null) {
                queue.offer(cur.left);
                queue.offer(cur.right);
            }
            else {
                break;
            }
        }
        while(!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            if(cur != null) {
                return false;
            }
        }
        return true;
    }

    //二叉树的最近的公共祖先1
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root == null) return null;

        if(p == root || q == root) return root;

        TreeNode leftRet = lowestCommonAncestor(root.left,p,q);
        TreeNode rightRet = lowestCommonAncestor(root.right,p,q);

        if(leftRet != null && rightRet != null) {
            return root;
        }
        else if(leftRet != null) {
            return leftRet;
        }
        else {
            return rightRet;
        }
    }



    //获取路径
    public boolean getPath(TreeNode root, TreeNode node, Stack<TreeNode> stack) {
        if(root == null) return false;

        stack.push(root);
        if(root == node) return true;

        boolean ret = getPath(root.left,node,stack);
        if(ret == true) return true;

        boolean ret2 = getPath(root.right,node,stack);
        if(ret2 == true) return true;

        stack.pop();
        return false;

    }


    //二叉树的最近的公共祖先2
    public TreeNode lowestCommonAncestor2(TreeNode root,TreeNode p,TreeNode q) {
        if (root == null) return null;

        Stack<TreeNode> stack1 = new Stack<>();
        getPath(root, p, stack1);

        Stack<TreeNode> stack2 = new Stack<>();
        getPath(root, q, stack2);

        int size1 = stack1.size();
        int size2 = stack2.size();

        if (size1 > size2) {
            int size = size1 - size2;
            while (size != 0) {
                stack1.pop();
                size--;
            }
        } else {
            int size = size2 - size1;
            while (size != 0) {
                stack2.pop();
                size--;
            }
        }
        //此时两个栈中的大小是一样的
        while (!stack1.isEmpty() && !stack2.isEmpty()) {
            TreeNode tmp1 = stack1.pop();
            TreeNode tmp2 = stack2.pop();
            if (tmp1 == tmp2) {
                return tmp1;
            }
        }
        return null;
    }
}
